package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.*;
import com.hyyt.shopjsp.product.service.IShopProductParameterService;
import com.hyyt.shopjsp.product.service.IShopProductParameterValueService;
import com.hyyt.shopjsp.product.service.IShopProductTypeService;
import com.hyyt.shopjsp.redis.service.IRedisInitializingBasicDataService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/8/7.
 */

@Controller
@RequestMapping("/back/productParameter")
public class ShopProductParameterController extends BaseController {

    private Logger logger = Logger.getLogger(ShopProductParameterController.class);
    private String basePath = "/back/shop/product/productParameter/";

    @Resource
    private IShopProductParameterService shopProductParameterService;
    @Resource
    private IShopProductParameterValueService shopProductParameterValueService;
    @Resource
    private IRedisInitializingBasicDataService redisInitializingBasicDataService;
    @Resource
    private IShopProductTypeService shopProductTypeService;

    /**
     * 进入商品参数页
     * @param param
     * @return
     */
    @RequestMapping("gotoProductParameterPage")
    public String gotoProductParameterPage(Map<String, Object> param){
        param.put("productTypeId","16");//默认指定分类初始化右侧列表参数数据
        return basePath+"productParameterList";
    }

    /**
     * 列表显示商品参数
     * @param productTypeId     商品分类ID
     * @param page
     * @param paramName         参数名称
     * @param response
     */
    @RequestMapping(value = "listProductParameter")
    @ResponseBody
    public void listProductParameter(String productTypeId, String paramName,Page page, HttpServletResponse response){
        Map<String, Object> param = new HashMap<>();
        if(Utils.objectIsEmpty(productTypeId)){
            productTypeId = "16";
        }
        if (Utils.objectIsNotEmpty(paramName)){
            param.put("paramName",paramName.trim());
        }
        String  productTypeName = redisInitializingBasicDataService.getProductTypeNameById(Integer.parseInt(productTypeId));
        param.put("productTypeId",productTypeId);
        int totalRecordCount = shopProductParameterService.countShopProductParameter(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<Map<String,Object>> shopProductParameterList = shopProductParameterService.queryProductParameterByProductTypeId(param, page.getPageRecordBeginIndex(), page.getPageSize());
        for(Map<String,Object> map:shopProductParameterList){
            map.put("productTypeName",productTypeName);
        }
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", shopProductParameterList);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 保存或修改商品参数
     * @param shopProductParameter
     * @return
     */
    @RequestMapping(value = "saveOrUpdateProductParameter")
    @ResponseBody
    public ResultMsg saveOrUpdateProductParameter(ShopProductParameter shopProductParameter){
        if(Utils.objectIsNotEmpty(shopProductParameter)){
            if(Utils.objectIsEmpty(shopProductParameter.getParamId())){
                shopProductParameterService.saveShopProductParameter(shopProductParameter);
            }else {
                shopProductParameterService.updateShopProductParameter(shopProductParameter);
            }
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 获取商品参数
     * @param paramId  商品参数ID
     * @return
     */
    @RequestMapping(value = "getProductParameterInfo")
    @ResponseBody
    public ResultMsg getProductParameterInfo(String paramId,Integer productTypeId){
        if(Utils.objectIsNotEmpty(paramId)){
            ShopProductParameter shopProductParameter = shopProductParameterService.loadShopProductParameter(Integer.parseInt(paramId));
            ShopProductType shopProductType = shopProductTypeService.loadShopProductType(shopProductParameter.getProductTypeId());
            Map<String,Object> param = new HashMap<>();
            param.put("paramId",paramId);
            List<ShopProductParameterValue> shopProductParameterValueList = shopProductParameterValueService.queryShopProductParameterValue(param);
            param.clear();
            param.put("shopProductParameter",shopProductParameter);
            param.put("shopProductType",shopProductType);
            param.put("shopProductParameterValueList", shopProductParameterValueList);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(param));
        }else if(Utils.objectIsNotEmpty(productTypeId)){
            ShopProductType shopProductType = shopProductTypeService.loadShopProductType(productTypeId);
            Map<String,Object> param = new HashMap<>();
            param.put("shopProductType",shopProductType);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(param));
        }
        return ResultMsg.create(false);
    }

    /**
     * 删除商品参数（只可删除商品参数下没有参数值的）
     * @param paramId
     * @return
     */
    @RequestMapping(value = "deleteProductParameter")
    @ResponseBody
    public ResultMsg deleteProductParameter(String paramId){
        if(Utils.objectIsNotEmpty(paramId)){
            Map<String,Object> param = new HashMap<>();
            param.put("paramId",paramId);
            int countShopProductParameterValue = shopProductParameterValueService.countShopProductParameterValue(param);
            if(countShopProductParameterValue==0){
                shopProductParameterService.deleteShopProductParameter(Integer.parseInt(paramId));
                return ResultMsg.create(true);
            }
        }
        return ResultMsg.create(false);
    }

    /**
     * 移除商品参数值
     * @param paramValueId
     * @return
     */
    @RequestMapping(value = "deleteShopProductParameterValue",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteShopProductParameterValue(Integer paramValueId){
        if(Utils.objectIsNotEmpty(paramValueId)){
            shopProductParameterValueService.deleteShopProductParameterValue(paramValueId);
            return ResultMsg.create(true);
        }
        return ResultMsg.create(false);
    }

    /**
     * 检测商品参数名
     * @param paramName         参数名称
     * @param paramId           参数ID
     * @param productTypeId     商品分类ID
     * @return
     */
    @RequestMapping(value = "checkName",method = RequestMethod.POST)
    @ResponseBody
    public Map checkName(String paramName,String productTypeId,Integer paramId){
        Map<String, Object> param = new HashMap<>();
        boolean flag=false;
        if (Utils.objectIsNotEmpty(paramName)) {
            param.put("paramName", paramName.trim());
            param.put("productTypeId",productTypeId);
            int countShopProductParameter = shopProductParameterService.countShopProductParameter(param); //统计同名字的参数个数
            if (countShopProductParameter > 0) {
                if(Utils.objectIsNotEmpty(paramId)){
                    if(shopProductParameterService.loadShopProductParameter(paramId).getParamName().equals(paramName)){ //区分当前参数
                        flag = true;
                    }
                }else {
                    flag = false;
                }
            }else {
                flag = true;
            }
        }
        param.clear();
        param.put("valid",flag);//bootstrap validator 远程校验需要返回{"valid",true}
        return param;
    }

}
